import Layer from '../../core/Layer';
import GridLayer from '../../layer/GridLayer';


export default class extends Layer {
    constructor(canvas, style) {
        super(canvas, style);
        this.series = [];
        this.xAxis = null;
        this.yAxis = null;
        this.shift = style.shift || 0;
        this.count = style.count || 30;
    }

    setXAxis(xAxis) {
        this.xAxis = xAxis;
    }

    setYAxis(yAxis) {
        this.yAxis = yAxis;
    }

    addSeries(line) {
        this.series.push(line);
    }

    build() {
        this.childs.splice(0);
        let min = Number.MAX_VALUE;
        let max = Number.MIN_VALUE;
        for (let i =0; i < this.series.length; i++) {
            const end = this.series[i].data.length - 1 - this.shift;
            const start = end - this.count;
            this.series[i].start = start;
            this.series[i].end = end;
            this.series[i].getMinMax();
            if (min > this.series[i].min) {
                min = this.series[i].min;
            }
            if (max < this.series[i].max) {
                max = this.series[i].max;
            }
        }
        const axisHeight = this.height - this.paddingTop - this.paddingBottom - this.xAxis.height;
        const yStep = axisHeight / (max - min);
        const axisMin = min - this.paddingBottom / yStep;
        const axisMax = max + this.paddingTop / yStep;
        this.yAxis.max = axisMax;
        this.yAxis.min = axisMin;
        this.yAxis.height = this.height - this.xAxis.height;
        this.yAxis.bottom = this.xAxis.height;
        this.xAxis.width = this.width - this.yAxis.width;
        this.xAxis.left = this.yAxis.width;
        const xStep = this.xAxis.width / this.count;
        this.xAxis.xStep = xStep;
        const grid = new GridLayer(this.canvas, {
            width: this.xAxis.width,
            height: this.yAxis.height,
            left: this.yAxis.width,
            bottom: this.xAxis.height,
            position: 'absolute',
            color: this.color || '#D3D3D3',
            lineWidth: 1,
            lineDash: [8, 4, 8],
            cols: this.xAxis.cols,
            rows: this.yAxis.rows,
        });
        this.addChild(this.yAxis);
        this.addChild(this.xAxis);
        this.addChild(grid);
        for (let i = 0; i < this.series.length; i++) {
            const series = this.series[i];
            series.height = (series.max - series.min) / (axisMax - axisMin)
                * (this.height - this.paddingTop - this.paddingBottom - this.xAxis.height);
            series.width = this.width - this.yAxis.width;
            series.left = this.yAxis.width;
            series.xStep = xStep;
            series.bottom = Math.round(this.xAxis.height + (series.min - axisMin) * yStep);
            const end = series.data.length - 1 - this.shift;
            const start = end - this.count + 1;
            series.start = start;
            series.end = end;
            this.addChild(series);
        }
    }
}
